package com.xjh.him.controller;

import com.xjh.him.annotation.LoginVerify;
import com.xjh.him.dto.friendgroup.FriendGroupDTO;
import com.xjh.him.dto.usergroup.UserGroupDTO;
import com.xjh.him.service.FriendGroupServiceImpl;
import com.xjh.him.service.UserGroupServiceImpl;
import com.xjh.him.vo.userseesion.UserSessionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xjh.tool.dto.ResultDTO;
import org.xjh.tool.vo.AjaxResultVO;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user/session")
public class UserSessionController {

    @Autowired
    private FriendGroupServiceImpl friendGroupService;

    @Autowired
    private UserGroupServiceImpl userGroupService;


    @LoginVerify
    @RequestMapping("/list")
    public AjaxResultVO listGroup() {
        List<UserSessionVO> voList = new ArrayList<>();
        ResultDTO<List<FriendGroupDTO>> friendGroupResultDTO = friendGroupService.listGroup();
        if (friendGroupResultDTO.isSuccess()) {
            List<FriendGroupDTO> dtoList = friendGroupResultDTO.getResult();
            for (FriendGroupDTO dto : dtoList) {
                UserSessionVO sessionVO = new UserSessionVO();
                voList.add(sessionVO);
                BeanUtils.copyProperties(dto, sessionVO);
                sessionVO.setTargetId(dto.getId());
                sessionVO.setGroupType(UserSessionVO.GROUP_TYPE_SINGLE);
            }
        }
        ResultDTO<List<UserGroupDTO>> userGroupResultDTO = userGroupService.listGroup();
        if (userGroupResultDTO.isSuccess()) {
            List<UserGroupDTO> dtoList = userGroupResultDTO.getResult();
            for (UserGroupDTO dto : dtoList) {
                UserSessionVO sessionVO = new UserSessionVO();
                voList.add(sessionVO);
                BeanUtils.copyProperties(dto, sessionVO);
                sessionVO.setTargetId(dto.getId());
                sessionVO.setGroupType(UserSessionVO.GROUP_TYPE_MULTIPLE);
            }
        }
        return AjaxResultVO.getSuccessResult(voList);
    }
}
